package com.clusterswisdom.horn.api.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.clusterswisdom.horn.api.db.cursor.TaskItemCursor;

public class TaskDbUtil {

	public static final String SQL_TASKSTATE = "update t_task set task_state = ? where task_id = ?";
	
	public static final String SQL_FETCHITEMS = "select * from t_task_item where item_task = ?";
	
	public static void updateTaskState(Long taskId, Long state) {
		Connection conn = null;
		try {
			conn = DbUtil.getUtil().getConnection();
			PreparedStatement prepareStatement = conn.prepareStatement(SQL_FETCHITEMS);
			prepareStatement.setLong(1, state);
			prepareStatement.setLong(2, taskId);
			
			prepareStatement.executeUpdate(SQL_TASKSTATE);
			conn.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
		}
	}
	
	public static TaskItemCursor getTaskItemsConnection(Long taskId) throws SQLException {
		Connection conn = DbUtil.getUtil().getConnection();
		PreparedStatement prepareStatement = conn.prepareStatement(SQL_FETCHITEMS);
		prepareStatement.setLong(1, taskId);
		
		ResultSet resultSet = prepareStatement.executeQuery(SQL_FETCHITEMS);
		return new TaskItemCursor(conn, resultSet);
	}
	
}
